package view;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import controler.CardBoxListener;
import controler.CardListener;
import main.Game;
import model.Card;
import model.City;
import model.Player;

public class Card_board_view extends JFrame{
	JPanel cardsBoard;
	JScrollPane fenetre;
	JComboBox<String>players;
	Player banque = new Player("bank",null);
	Player player;
	JPanel board;
	public ArrayList<City>c=new ArrayList<>();
	City city;

	Game game;
	public Card_board_view(Game g) {
		this.game=g;
		
		players= new JComboBox<>();
		players.setFont(new Font("Arial", Font.BOLD, 16));
		players.addItem(banque.getName());

		for(int i=0; i<g.listPlayer.size();i++){
			players.addItem(g.listPlayer.get(i).getName());
		}
		players.addActionListener(new CardBoxListener(this,game));
		for(int i=0;i<game.bank.city.size();i++){
			c.add((City)game.bank.city.get(i));
		}
		view();
	}

	public void view(){
		board = new JPanel(new GridBagLayout());
		GridBagConstraints gb = new GridBagConstraints();
		cardsBoard = new JPanel();
		cardsBoard.setBorder(BorderFactory.createTitledBorder("Cards"));
		cardsBoard.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		JButton close = new JButton("Close this window");
		close.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				close();
			}
		});
		fenetre = new JScrollPane(cardsBoard);
		fenetre.setPreferredSize(new Dimension(850,550));
		fenetre.getVerticalScrollBar().setUnitIncrement(90);
		int p,cy,m,o,r,y,g,b,st,ut;
		p=cy=m=o=r=y=g=b=st=ut=0;
		gbc.insets=new Insets(0, 10, 10, 10);
		
		if(c.size()>0){
			for(int i=0;i<c.size();i++){
				if(c.get(i).family.equals("pink")){
					gbc.gridx=p;
					gbc.gridy=0;
					p++;
				}
				else if(c.get(i).family.equals("cyan")){
					gbc.gridx=cy;
					gbc.gridy=1;
					cy++;
				}
				else if(c.get(i).family.equals("magenta")){
					gbc.gridx=m;
					gbc.gridy=2;
					m++;
				}
				else if(c.get(i).family.equals("orange")){
					gbc.gridx=o;
					gbc.gridy=3;
					o++;
				}
				else if(c.get(i).family.equals("red")){
					gbc.gridx=r;
					gbc.gridy=4;
					r++;
				}
				else if(c.get(i).family.equals("yellow")){
					gbc.gridx=y;
					gbc.gridy=5;
					y++;
				}
				else if(c.get(i).family.equals("green")){
					gbc.gridx=g;
					gbc.gridy=6;
					g++;
				}
				else if(c.get(i).family.equals("blue")){
					gbc.gridx=b;
					gbc.gridy=7;
					b++;
				}
				else if(c.get(i).family.equals("station")){
					gbc.gridx=st;
					gbc.gridy=8;
					st++;
				}
				else {
					gbc.gridx=ut;
					gbc.gridy=9;
					ut++;
				}
				cardsBoard.add((new Card_view(c.get(i))),gbc);				
			}
		}
		gb.gridx=0;
		gb.gridy=0;
		gb.insets = new Insets(0, 0, 20, 0);
		board.add(players,gb);
		gb.gridy=1;
		//gb.insets = new Insets(0, 0, 0, 0);
		board.add(fenetre,gb);
		gb.gridy=2;
		board.add(close,gb);
		this.setContentPane(board);
		this.setTitle("Card Board");//Title of the Frame
		this.setExtendedState(JFrame.MAXIMIZED_BOTH);
		this.setLocationRelativeTo(null);//center the frame
		this.setVisible(true);//Set the frame visible
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}
	private void close(){
		this.dispose();
	}
}
